package algorithm.niuke;

/**
 * 替换空格
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020-05-06
 */
public class SpaceReplace {

    public static void main(String[] args) {

        String str = "We Are Happy";
        String replaceStr = replace(str);
        System.out.println(replaceStr);

    }

    /**
     * 将一个字符串中的每个空格替换成“%20”
     *
     * @param str 要替换的字符串
     * @return 空格被替换之后的字符串
     */
    private static String replace(String str) {

        StringBuilder sb = new StringBuilder();
        char[] strArray = str.toCharArray();
        for (char c : strArray) {
            if (c == 32) {
                sb.append("%20");
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}
